#!/usr/bin/env python 
# -*- coding:utf-8 -*-

import pymysql
from influxdb import InfluxDBClient


class DBConn:
    """数据库连接类
    输入：字典类型（包括：url,port,username,password,database）
    输出：conn(连接),cur(游标)
    """

    def __init__(self, **kwargs):
        """Init DBConn"""
        for k, v in kwargs.items():
            if k == 'host':
                self.url = v
            if k == 'port':
                self.port = v
            if k == 'username':
                self.username = v
            if k == 'password':
                self.password = v
            if k == 'database':
                self.database = v

    def influx_conn(self):
        """Influxdb 连接方法"""
        client = InfluxDBClient(self.url,
                                self.port,
                                self.username,
                                self.password,
                                self.database,
                                timeout=10)
        return client

    def mysql_conn(self):
        """MySQL 连接方法"""
        conn = pymysql.connect(host=self.url,
                               port=self.port,
                               user=self.username,
                               passwd=self.password,
                               db=self.database,
                               charset='utf8')
        cursor = conn.cursor()
        return conn, cursor
